1. /* sdfatan2.cpp by K.Tsuru */
  2. // function ID = 3110 DARDIX
  3. /*******************************************************
  4. A bug for x = 0 was removed in version 2.14.
  5. SDouble class
  6. inverse trigonometric function arctan(y/x)
  7. SDouble version of atan2(double y,double x)
  8. Return an azimuth angle of the point (x,y) on the xy-plane
  9. within the region (-pi,pi].
  10. ********************************************************/
  11. #ifndef SN_H
  12. #include "sn.h"
  13. #endif
  14. #if 1
  15. SDouble Atan2(const SDouble& y, const SDouble& x){
  16. SDouble r;
  17. if(x.Sign() == 0){
  18. r = MPi2(); // r = pi/2
  19. if(y.Sign() < 0) r.ChangeSign(); // r = -pi/2
  20. if(y.Sign() == 0) r.SetError(x.DOMAIN_ERR, "Atan2", -3110);
  21. return r;
  22. }
  23. //x != 0
  24. if(y.Sign() == 0){
  25. if(x.Sign() > 0) return 0.0;
  26. return Pi();
  27. }
  28. r = y/x;
  29. r = Atan(r);
  30. if(x.Sign() > 0); // r = Atan(r); 1st or 4th
  31. else if(r.Sign() < 0) r += Pi(); // x<0, y >0, r = Pi() + Atan(r); 2nd
  32. else r -= Pi(); // r = Atan(r) - Pi(); 3rd or y = r = 0
  33. return r;
  34. }
  35. #else
  36. static SDouble pi= Pi();
  37. SDouble Atan2(const SDouble& y, const SDouble& x){
  38. SDouble r;
  39. if(x.Sign() == 0){
  40. r = MPi2(); // r = pi/2
  41. if(y.Sign() < 0) r.ChangeSign(); // r = -pi/2
  42. if(y.Sign() == 0) r.SetError(x.DOMAIN_ERR, "Atan2", -3110);
  43. return r;
  44. }
  45. //x != 0
  46. if(y.Sign() == 0){
  47. if(x.Sign() > 0) return 0.0;
  48. return Pi();
  49. }
  50. r = y/x;
  51. r = Atan(r);
  52. if(x.Sign() > 0); // r = Atan(r); 1st or 4th
  53. else if(r.Sign() < 0) r += pi; // x<0, y >0, r = Pi() + Atan(r); 2nd
  54. else r -= pi; // r = Atan(r) - Pi(); 3rd or y = r = 0
  55. return r;
  56. }
  57. #endif

sdfatan2.cpp : last modifiled at 2015/06/28 15:26:28(1,602 bytes)
created at 2017/10/07 10:22:50
The creation time of this html file is 2017/10/07 11:29:39 (Sat Oct 07 11:29:39 2017).